package org.jsola.contract.helper;

import org.jsola.contract.dto.ContractCredentialAddDTO;
import org.jsola.contract.dto.ContractCredentialUpdateDTO;
import org.jsola.contract.entity.ContractCredentialDO;
import org.jsola.contract.query.ContractCredentialQuery;
import org.jsola.contract.service.IContractCredentialService;
import org.jsola.contract.vo.ContractCredentialVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.contract.core.AbstractTest.SITE_ID;



/**
 * 电子合同账号凭证
 *
 * @author JanusMix
 */
@Component
public class ContractCredentialTestHelper extends BaseTestHelper {
    
    @Autowired
    private IContractCredentialService contractCredentialService;

    /**
     * 生成add测试数据
     * @return ContractCredentialAddDTO
     */
    public ContractCredentialAddDTO getContractCredentialAddDTO(){
        ContractCredentialAddDTO contractCredentialAddDTO = new ContractCredentialAddDTO();
        contractCredentialAddDTO.setName(getRandomValue("varchar",255));
        contractCredentialAddDTO.setThirdChannel(getRandomValue("varchar",255));
        contractCredentialAddDTO.setProjectId(getRandomValue("varchar",255));
        contractCredentialAddDTO.setSecret(getRandomValue("varchar",255));
        contractCredentialAddDTO.setApiHost(getRandomValue("varchar",255));
        contractCredentialAddDTO.setApiUrl(getRandomValue("varchar",255));
        return contractCredentialAddDTO;
    }


    /**
     * 生成update测试数据
     * @return ContractCredentialUpdateDTO
     */
    public ContractCredentialUpdateDTO getContractCredentialUpdateDTO(ContractCredentialVO contractCredentialVO){
        return contractCredentialService.selectDOById(contractCredentialVO.getId(), SITE_ID).to(ContractCredentialUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return ContractCredentialQuery
     */
    public ContractCredentialQuery getContractCredentialQuery() {
        ContractCredentialQuery contractCredentialQuery = new ContractCredentialQuery();
        List<ContractCredentialDO> contractCredentialDOList = contractCredentialService.list(new ContractCredentialDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(contractCredentialDOList)) {
            ContractCredentialDO contractCredentialDO = contractCredentialDOList.get(0);
            contractCredentialQuery = contractCredentialDO.to(ContractCredentialQuery.class);
        }
        return contractCredentialQuery;
    }

    /**
     * 保存示例
     * @return ContractCredentialVO
     */
    public ContractCredentialVO saveContractCredentialExample(){
        ContractCredentialAddDTO addDTO = this.getContractCredentialAddDTO();
        ContractCredentialVO contractCredentialVO = contractCredentialService.save(addDTO,buildTokenUser());
        return contractCredentialVO;
    }


}

